from rest_framework import serializers
from apps.article.models import Channel, Article, Comment
from apps.users.models import User


class GetChannelsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = '__all__'


class UserSimpleArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ('id', 'title')


class ArticleUserSerializer(serializers.ModelSerializer):
    articles = UserSimpleArticleSerializer(label='文章作者对应的文章信息', many=True)
    fans = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'articles', 'fans')


class ArticleListSerializer(serializers.ModelSerializer):
    user = ArticleUserSerializer(label='文章对应的作者信息')
    collected_users = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
    collected = serializers.BooleanField(read_only=True, default=False)

    class Meta:
        model = Article
        fields = ('id', 'title', 'content', 'createtime', 'user', 'collected_users', 'image', 'collected')


class ArticleCommentsSubsSerializer(serializers.ModelSerializer):
    subs = serializers.StringRelatedField(many=True)
    user = ArticleUserSerializer(label='文章对应的作者信息')

    class Meta:
        model = Comment
        fields = '__all__'


class ArticleCommentsSerializer(serializers.ModelSerializer):
    user = ArticleUserSerializer(label='文章对应的作者信息')
    subs = ArticleCommentsSubsSerializer(many=True)

    class Meta:
        model = Comment
        fields = '__all__'


class ArticleDetailSerializer(serializers.ModelSerializer):
    user = ArticleUserSerializer(label='文章对应的作者信息')
    comments = ArticleCommentsSerializer(label='文章的评论', many=True)
    labels = serializers.PrimaryKeyRelatedField(label='标签', many=True, read_only=True)
    collected_users = serializers.PrimaryKeyRelatedField(read_only=True, label='收藏用户姓名', many=True)

    class Meta:
        model = Article
        fields = '__all__'


class PostArticleSerializer(serializers.ModelSerializer):
    user_id = serializers.IntegerField(write_only=True)

    class Meta:
        model = Article
        fields = ('id', 'user_id', 'content', 'labels', 'title', 'channel', 'image', 'createtime')
        extra_kwargs = {
            'image': {'required': False, },
            'createtime': {'read_only': True},
        }

    def create(self, validated_data):
        labels = validated_data.pop('labels')
        article = Article.objects.create(**validated_data)
        for label in labels:
            article.labels.add(label)
        return article


from drf_haystack import serializers
from .search_indexes import ArticleIndex


class ArticleSearchSerializer(serializers.HaystackSerializer):

    """
    索引结果数据序列化器
    """
    object = ArticleListSerializer(read_only=True)

    class Meta:
        index_classes = [ArticleIndex]
        # text 由索引类进行返回, object 由序列化类进行返回,第一个参数必须是text
        fields = (
            # 'text',  # 用于接收查询关键字
            # 'object',  # 用于返回查询结果
        )
